<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}"
            @reset="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="项目名称" name="projectName">
            <t-input
              v-model="queryParams.projectName"
              clearable
              placeholder="请输入项目名称"
              style="width: 240px"
            />
          </t-form-item>
          <!--          <t-form-item label="变更类型" name="subjectClassification">-->
          <!--            <t-select style="width: 240px" clearable v-model="queryParams.changeType"-->
          <!--                      :options="dict.type.change_type">-->
          <!--            </t-select>-->
          <!--          </t-form-item>-->
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="toApply" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增项目执行情况
      </t-button>
    </div>
  <t-table
    rowKey="id"
    :data="data"
    :columns="columns"
    hover
    :pagination="pagination"
    :loading="dataLoading"
  >
    <template #[`project.projectName`]="{ row }">
      <t-link @click="dialog.objId=row.projectId;dialog.header=row.project.projectName;dialog.open_project_detail=true" hover="color"
              theme="primary">{{ row.project.projectName }}
      </t-link>
    </template>
    <template #[`project.adviceFunding`]="{ row }">
      {{ row.project.adviceFunding }}万元
    </template>
    <template #[`project.projectStatus`]="{ row }">
      <t-tag @click="dialog.header=row.project.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true"
             class="cursor-pointer" :theme="handleTheme(row.project.projectStatus, dict.type.sf_project_status)"
             variant="light">
        {{ row.project.projectStatus | filterByDict(dict.type.sf_project_status) }}
      </t-tag>
    </template>
    <template #status="{ row }">
      <t-tag @click="dialog.header=row.project.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true" class="cursor-pointer" v-if="row.status" :theme="handleTheme(row.status,dict.type.approve_status)" variant="light">
        {{ row.status | filterByDict(dict.type.approve_status) }}
      </t-tag>
      <t-tag @click="dialog.header=row.project.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true" class="cursor-pointer" v-else theme="warning" variant="light">未提交</t-tag>
    </template>
    <template #op="{row, rowIndex}">
      <t-button v-if="['1','4',null].includes(row.status)" @click="toUpdate(row)" size="small"
                variant="text" class="t-button-link">
        <t-icon name="edit-1" slot="icon"/>
        继续填报
      </t-button>
      <t-button size="small" @click="dialog.header=row.project.projectName;dialog.objId=row.id;dialog.projectId=row.projectId;dialog.open_detail=true"  variant="text" class="t-button-link">
        <t-icon name="info-circle" slot="icon"/>
        详情
      </t-button>
      <t-button @click="createPDF(row)" size="small"
                variant="text" class="t-button-link">
        <t-icon name="file-pdf" slot="icon"/>
        导出PDF
      </t-button>
      <t-button  v-if="['1','4', null].includes(row.status)" theme="danger" @click="delObj(row)" size="small" variant="text" class="t-button-link">
        <t-icon name="delete" slot="icon"/>
        删除
      </t-button>
    </template>
  </t-table>
    <project-detail-dialog tab-default-value="3" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
                           v-if="dialog.open_project_detail"></project-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approve-list>
    <performance-report-detail-dialog :header="dialog.header" :objId="dialog.objId" :project-id="dialog.projectId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></performance-report-detail-dialog>
  </div>
</template>

<script>
import projectDetailDialog from "../project/components/project-detail-dialog.vue";
import ApproveList from "../approve/components/approve-list.vue";
import performanceReportDetailDialog from "./components/performance-report-detail-dialog.vue";

export default {
  name: "performance-report-my",
  dicts: ['approve_status','sf_project_status'],
  components:{
    projectDetailDialog,
    ApproveList,
    performanceReportDetailDialog
  },
  data(){
    return {
      dialog: {
        open_project_detail: false,
        open_approve_list: false,
        header: "",
        objId: null,
        open: false,
        open_detail: false,
        projectId: null,

      },
      data: [],
      columns: [
        {
          colKey: "project.projectName",
          title: "项目名称",
          align: "center",
          width: 220
        },
        {
          colKey: "project.adviceFunding",
          title: "投入经费",
          align: "center",
          width: 160,
        },
        {
          colKey: "projectLeaderName",
          title: "项目负责人",
          align: "center",
          width: 160,
        },
        {
          colKey: "project.manageDeptName",
          title: "主管单位",
          align: "center",
          width: 160,
        },
        {
          colKey: "declareDeptName",
          title: "申报单位",
          align: "center",
          width: 160,
        },
        {
          colKey: "project.declareDate",
          title: "申报日期",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "project.startDate",
          title: "项目开始日期",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "project.endDate",
          title: "项目结束日期",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "project.projectStatus",
          title: "项目状态",
          align: "center",
          width: 160,
          fixed: 'right'
        },
        {
          colKey: "status",
          title: "审批状态",
          align: "center",
          width: 180,
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 180,
          fixed: 'right'
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: '',
        changeType: '',
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10,20,30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created(){
    this.getList();
  },
  methods:{
    createPDF(row){
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kjsf.implement.performanceReportPDF(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    getList(){
      this.dataLoading = true;
      this.$api.kjsf.implement.performanceReportMyList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    toUpdate(row){
      sessionStorage.removeItem(`${row.id}-performance-report-activeForm`)
      const taskId = row.id;
      const activeFormList = [];
      this.$api.kjsf.implement.getPerformanceReportAll(row.id).then(response => {
        sessionStorage.removeItem(`${taskId}-performance-report-step0`);
        sessionStorage.removeItem(`${taskId}-performance-report-projectId`);
        if(response.data.id){
          sessionStorage.setItem(`${taskId}-performance-report-step0`, response.data.id);
          sessionStorage.setItem(`${taskId}-performance-report-projectId`, row.projectId);
        }else {
          activeFormList.push(0);
        }
        if (response.data.projectAnnualReportPlan && response.data.projectAnnualReportPlan.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step1`, response.data.projectAnnualReportPlan.id)
        } else {
          activeFormList.push(1);
        }
        if (response.data.projectAnnualReportGetResults && response.data.projectAnnualReportGetResults.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step2`, response.data.projectAnnualReportGetResults.id)
        } else {
          activeFormList.push(2);
        }
        if (response.data.projectAnnualReportExperience && response.data.projectAnnualReportExperience.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step3`, response.data.projectAnnualReportExperience.id)
        } else {
          activeFormList.push(3);
        }
        if (activeFormList.length <= 0) {
          sessionStorage.setItem(`${taskId}-performance-report-activeForm`, 3); // 跳转到最后一步
        } else {
          sessionStorage.setItem(`${taskId}-performance-report-activeForm`, activeFormList[0]); // 跳转到第一个不满足
        }
        this.$router.push({path:`/kjsf/other/implement/performance-report-apply/${row.id}`,query:{title:`${row.project.projectName}-项目执行情况表`}});
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    toApply(){
      this.$router.push({
        path: `/kjsf/other/implement/performance-report-apply/${Date.parse(new Date())}`,
      })
    },
    delObj(row){
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除当前数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.kjsf.implement.performanceReportDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
